package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Present;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IPresentService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//礼品
@Controller
@RequestMapping("present")
public class PresentController {

    @Autowired
    private IPresentService presentService;

    @RequiresPermissions("present:index")
    @PermissionName("礼品首页")
    @RequestMapping("")
    public String index(){
        presentService.list();
        return "present/list";
    }

    //礼品信息列表/带分页
    @RequiresPermissions("present:list")
    @PermissionName("礼品列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo){
        return presentService.query(qo);
    }


    //所有礼品信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return presentService.list();
    }


    //礼品添加
    @RequiresPermissions("present:add")
    @PermissionName("礼品添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Present entity){
        JSONResult result = new JSONResult();
        try {
            presentService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result ;
    }

    //礼品编辑
    @RequiresPermissions("present:edit")
    @PermissionName("礼品编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Present entity){
        JSONResult result = new JSONResult();
        try {
            presentService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result ;
    }

    //礼品删除
    @RequiresPermissions("present:delete")
    @PermissionName("礼品删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            presentService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result ;
    }

    @RequestMapping("getPresent")
    @ResponseBody
    public Object getPresent(Long id){
        return presentService.get(id);
    }
}
